/* ===========================================================================
 *
 *       Filename:  simp_sync_mutx.h
 *
 *    Description:
 *
 * - Mode: 644
 * - NOTE. NOT use "dos"
 * - Updated: TODO
 *
 *        Version:  1.0.0
 *        Created:  2014-11-07 16:55:35
 *       Revision:  1.0.0
 *       Compiler:  ...gcc/g++
 *
 *         Author:  Yui Wong, email: yuiwong@126.com
 *   Organization:  ""
 *        License:  LGPLv3
 *
 *	This file is is free software: you can redistribute it
 *  and/or modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *	This file
 *  is distributed in the hope that it will be
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License along
 *  with this file.
 *	If not, see <http://www.gnu.org/licenses/>.
 *
 * ===========================================================================
 */


#if !defined(_SIMP_SYNC_MUIX_H)
#define _SIMP_SYNC_MUIX_H (1)


#include <stdint.h> /* uint8_t */
#include "usleep.h"
#include "struct_timespec_t.h"


#if defined(__cplusplus)
extern "C"
{
#endif /* __cplusplus */


/*
 * simple cond -- 'pv'
 */
extern int op_p_capture(volatile int * cond);/* - and wait */
extern int op_v_release(volatile int * cond);/* no wait and + */


extern int op_p_nowait(volatile int * cond);/* - and no wait */


/*
 * op_p_capture_timeout
 *
 * -- arg(s):
 * - NULL timeout, wait till ok
 * - NOT NULL timeout, wait till timeout ok ok
 *
 * -- return:
 * - ok: 0
 * - fail: -1 and set errno (NULL cond or timeout)
 */
extern int op_p_capture_timeout(volatile int * cond,
	const struct timespec * timeout);


/*
 * if 0: NOT op_p_capture.. or => < -1
 */
#define op_p_capture_try(__0_vltl_int_cond) (\
	(__0_vltl_int_cond) >= 0)


/*
 * op_p_capture_try_timeout
 *
 * -- arg(s):
 * - NULL timeout, wait till ok
 * - NOT NULL timeout, wait till timeout ok ok
 * - XXX-NOTE:
 * . will NOT always lock
 * . ok only when *cond >= 1
 *
 * -- return:
 * - ok: 0
 * - fail: -1 and set errno (NULL cond-or-timeout or timeout)
 */
extern int op_p_capture_try_timeout(volatile int * cond,
	const struct timespec * timeout);


/*
 * simple mutex lock
 */
#define __PERUSLEEP (10 * 1e3) /* 10 ms */
#define capture_lock(__0_vltl_uint8_mutex) do { \
		while (0 == __0_vltl_uint8_mutex) { \
			usleep(__PERUSLEEP); \
		} \
		__0_vltl_uint8_mutex = 0; \
	} while (0)

#define rel_lock(__0_vltl_uint8_mutex) do { \
		__0_vltl_uint8_mutex = 1; \
	} while (0)


/*
 * capture_lock_timeout
 *
 * -- arg(s):
 * - NULL timeout, wait till ok
 * - NOT NULL timeout, wait till timeout ok ok
 *
 * -- return:
 * - ok: 0
 * - fail: -1 and set errno (NULL cond or timeout)
 */
extern int capture_lock_timeout(volatile uint8_t * mutex,
	const struct timespec * timeout);
/*
 * ===========================================================================
 * end of global funcs
 * ===========================================================================
 */
#if defined(__cplusplus)
}
#endif /* __cplusplus */


#endif /* _SIMP_SYNC_MUIX_H */


/*
 * end of file simp_sync_mutx.h
 */
